local skynet = require "skynet"
local cluster = require "skynet.cluster"
local runconfig = require "runconfig"

skynet.start(function()
    local mynode = skynet.getenv("node")
	local nodecfg = runconfig[mynode]

    local gateserver_host = skynet.getenv("gateserver_host") or "0.0.0.0"
    local gateserver_port = tonumber(skynet.getenv("gateserver_port")) or 8888
    local debug_port = tonumber(skynet.getenv("debug_console_port"))


    local protoloader = skynet.uniqueservice("protoloader")
    skynet.call(protoloader, "lua", "load", {
        "c2s.sproto", 
        "s2c.sproto",
    })

    if not skynet.getenv "daemon" then
		local console = skynet.newservice("console")
	end
	skynet.newservice("debug_console", debug_port)

    local logind = skynet.uniqueservice("login")
    local gated = skynet.uniqueservice("gate")
    local watchdog = skynet.uniqueservice("watchdog")
    
    skynet.call(watchdog, "lua", "start", {
        address = gateserver_host, -- gateserver: CMD.open
        port = gateserver_port,
        nodelay = true, 
        maxclient = 1024,

        servername = nodecfg.gate.servername,
        gated = gated,
        logind = logind,
    })

    skynet.exit()
end)
